﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.IO;

namespace CGeers.Wcf.Services.Preprocesslanguage
{
    public class ProcessPragraph
    {

        private NgramN ng;
        private Ngram n;
        private static Dictionary<string, string> wLinked = new Dictionary<string, string>();
        private List<string> punctuation = new List<string>();
        private List<string> sentencesUS = new List<string>();
        private List<string> sentencesS = new List<string>();
        public static bool IsWordLinked { get; set; }
        public static bool IsNgramNew { get; set; }

        public static void readData(string path)
        {

            StreamReader sr = new StreamReader(path);
            string str = "";
            string[] temp = null;
            Console.WriteLine("Reading word linked data...");
            while ((str = sr.ReadLine()) != null)
            {
                temp = str.Split(':');
                if (wLinked.ContainsKey(temp[0]))
                   wLinked[temp[0]] = temp[1];
                else
                    wLinked.Add(temp[0], temp[1]);
            }
            Console.WriteLine("Read completed."+wLinked.Count);

        }
        private string wordLinkedProcess(string str)
        {
            Console.WriteLine("run "+str);
            string strUs = Utils.processUtils.ConvertToUnSign(str);
            string[] arrWord = str.Split(' ');
            string[] arrWordUS = strUs.Split(' ');
            string[] arrTemp = null;
            string temp = "", rs = "";
            for (int i = 0; i < arrWordUS.Length - 2; i++)
            {
                temp = arrWordUS[i] + " " + arrWordUS[i + 1];
                Console.WriteLine("T:"+temp);
                if (wLinked.ContainsKey(temp))
                {
                    Console.WriteLine("Test:"+temp);
                    arrTemp = wLinked[temp].Split(' ');
                    arrWord[i] = arrTemp[0];
                    arrWord[i + 1] = arrTemp[1];
                    i++;
                }
            }
            for (int i = 0; i < arrWord.Length; i++)
            {
                rs += arrWord[i] + " ";
            }
            rs.Remove(rs.Length - 1);
            return rs;
        }

        private static string PATTERN_PUNCTUATION = "[\n.,;“”:()!?'`\\\"/-] ?";

        public ProcessPragraph(NgramN ng,Ngram n)
        {
            this.ng = ng;
            this.n = n;
            IsWordLinked = false;
            IsNgramNew = false;
        }

        private void splitSentence(string str)
        {
            MatchCollection mc = Regex.Matches(str, PATTERN_PUNCTUATION);
            foreach (Match m in mc)
            {
                punctuation.Add(m.Value);
            }
            foreach (string str1 in Regex.Split(str, PATTERN_PUNCTUATION))
            {
                sentencesUS.Add(str1);
            }
        }
        public string process(string str)
        {
            string kq = "";
            string rsStr = "";
            splitSentence(str.ToLower());
            foreach (string sen in sentencesUS)
            {
                Dictionary<string, double> t = null;
                if (IsNgramNew)
                {
                    t = ng.process(sen);
                    rsStr = ng.getKq(t);
                }
                else
                {
                    t = n.process(sen);
                    rsStr = n.getKq(t);
                }
                if (IsWordLinked)
                {
                    rsStr = wordLinkedProcess(rsStr);
                }
                sentencesS.Add(rsStr);
            }
            if (sentencesS.Count > punctuation.Count)
            {
                for (int i = 0; i < punctuation.Count; i++)
                {
                    kq += sentencesS[i] + punctuation[i];
                }
                kq += sentencesS[sentencesS.Count - 1];
            }
            else
            {
                for (int i = 0; i < sentencesS.Count; i++)
                {
                    kq += sentencesS[i] + punctuation[i];
                }
            }
            sentencesUS.Clear();
            punctuation.Clear();
            sentencesS.Clear();
            return kq;
        }

    }
}
